/**
*  \file switch.c
*
*  \brief
*
*  \author Dajin Li
*
*
*/
/*===============================================[ private includes  ]================================================*/


#include "switch.h"
#include "../pal/pal_types.h"
#include "../pal/pal_mem.h"




/*===============================================[ private defines   ]================================================*/
#define SWITCH_SAMPLES                       4             // SAMPLES * execution rate = debounce time



/*===============================================[ private datatypes ]================================================*/




/*===============================================[ private variables ]================================================*/
static U32 debounce_buff[SWITCH_SAMPLES + 1];        // circular buffer
static U32 load_offset = 0;                 // offset variable for loading buffer
static U32 debounce_time = SWITCH_SAMPLES;           // Default debounce time = 1000ms
static U32 Switches_Ready_Count = 0;                 // Count to make sure we have valid switches at startup

/*===============================================[ public variables  ]================================================*/
U32 Switches_Ready = 0;                     // flag indicating sure we have valid switches at startup
U32 Switches_Current;

/*================================================[ inline functions ]================================================*/





/*===============================================[ private functions ]================================================*/

/*================================================[ public functions ]================================================*/



/**
* @brief    This samples the switch inputs.
* @param    none
* @note     none
* @retval   none
*/
void switch_inputs_sample(void) 
{
    U32 read_offset;      // offset variable for reading buffer
    U32 and_buff = 0xFFFFFFFF;          // variable for ANDding total buffer
    U32 or_buff = 0x00000000;           // variable for ORing total buffer

    
    and_buff = 0xFFFFFFFF;
    or_buff = 0x00000000;
    
    debounce_buff[ load_offset ] = 0;                          // load buffer and rotate offset
    if(load_offset < debounce_time)
    {
        load_offset++;
    }
    else
    {
        load_offset = 0;
        if (++Switches_Ready_Count >= 2)
        {
            Switches_Ready = 1;
        }
    }

    for(read_offset = debounce_time; read_offset ; read_offset-- )
    {
        or_buff   |= debounce_buff[read_offset];        // OR all buffer entries
        and_buff  &= debounce_buff[read_offset];        // AND all buffer entries
    }
    Switches_Current = (Switches_Current | and_buff) & or_buff;  // Get samples

}  // end task_sample_sw_inputs()  


 

/*===============================================[ private functions ]================================================*/


 

/***********************************************************************************************************************
*                             Copyright 2016 Linde Material Handling. All rights reserved.                             *
***********************************************************************************************************************/

